package net.zoneland.knowledge.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import net.zoneland.knowledge.controller.bean.ExcelSheetVo;
import net.zoneland.knowledge.controller.bean.SanHuiYiKeVo;
import net.zoneland.knowledge.model.DiYiYiTi;
import net.zoneland.knowledge.utils.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @version v1.0
 * @Description:
 * @ClassName: BaseController
 * @Author: wenz
 * @Date: 2021/12/30
 */
public class BaseController {

    /**
     * 设置excel下载响应头属性
     */
    protected void setExcelRespProp(final HttpServletResponse response, final String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        final String fileName = URLEncoder.encode(rawFileName, "UTF-8").replace("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 这步很重要，需要在给前端返回的请求头中添加Content-Disposition字段，否则前端会取不到Content-Disposition的信息
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }

    /**
     * EasyExcel 生成.
     * @param response
     * @param tClass
     * @param writeHandler
     * @param fileName
     * @param list
     * @param vo
     * @param <T>
     * @throws IOException
     */
    protected <T> void write(final HttpServletResponse response, final Class<T> tClass, final CellWriteHandler writeHandler,final String fileName,
                             final List<T> list, final SanHuiYiKeVo vo,
                             final List<Integer> indexes) throws IOException {
        EasyExcel.write(response.getOutputStream(), tClass)
                .excelType(ExcelTypeEnum.XLSX)
                .inMemory(true)
                .registerWriteHandler(writeHandler)//设置列宽
                .registerWriteHandler(new AddNoHandler())//序号自增长
                .registerWriteHandler(new CustomCellWriteHeightConfig((short)25,(short)25)) // 简单的行高策略：头行高，内容行高
                .registerWriteHandler(DiYiYiTi.setHorizontalCellStyleStrategy()) // 设置单元格头部及内容样式
                .registerWriteHandler(new WriteHandlerStrategy(vo.getMeetingNames(), indexes))//设置单元格内部分文字样式
                .sheet(fileName)
                .doWrite(list);

    }

    /**
     * EasyExcel 生成.
     * @param response
     * @param writeHandler
     * @param excelSheets
     * @param vo
     * @param <T>
     * @throws IOException
     */
    protected <T> void sanHuiYiKeWrite(final HttpServletResponse response, final CellWriteHandler writeHandler,
                                  final List<ExcelSheetVo> excelSheets, final SanHuiYiKeVo vo,
                                  final List<Integer> indexes) throws IOException {
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .excelType(ExcelTypeEnum.XLSX)
                .inMemory(true)
                .registerWriteHandler(writeHandler)//设置列宽
                .registerWriteHandler(new AddNoHandler())//序号自增长
                .registerWriteHandler(new CustomCellWriteHeightConfig((short)25,(short)25)) // 简单的行高策略：头行高，内容行高
                .registerWriteHandler(DiYiYiTi.setHorizontalCellStyleStrategy()) // 设置单元格头部及内容样式
                .registerWriteHandler(new WriteHandlerStrategy(vo.getMeetingNames(), indexes))//设置单元格内部分文字样式
                .build();

        // 循环添加多个sheet
        excelSheets.forEach(sheet -> {
            WriteSheet writeSheet = EasyExcelFactory.writerSheet(sheet.getSheetIndex(), sheet.getSheetName())
                    .head(sheet.getSheetClass())
                    .build();
            excelWriter.write(sheet.getSheetList(), writeSheet);
        });
        excelWriter.finish();

    }

}
